using System;
using System.Data;
using Utils;
using Utils.DatabaseUtils;


namespace QLBH
{
public class tblNguoiDungUI
{ 
	

    public NguoiDung Login(String taiKhoan, String matKhau)
    {
        try
        {
            NguoiDung result = toNguoiDung(SqlHelper.ExecuteDataset(Common.Conn, "tblNguoiDung_Login", taiKhoan, matKhau));
            return result;
        }
        catch (Exception e) { return null; }
    }

    private NguoiDung toNguoiDung(DataSet ds)
    {
        try
        {
            if (TableLib.HasData(ds))
            {
                DataRow row = ds.Tables[0].Rows[0];
                NguoiDung nd = new NguoiDung();
                nd.ChucVu = row["ChucVu"].ToString();
                nd.DiaChi = row["DiaChi"].ToString();
                nd.DiDong = row["DiDong"].ToString();
                nd.DienThoai = row["DienThoai"].ToString();
                nd.HoTen = row["HoTen"].ToString();
                nd.IdBoPhan = Convert.ToInt32(row["IdBoPhan"]);
                nd.IdCuaHang = Convert.ToInt32(row["IdCuaHang"]);
                nd.IdNguoiDung = Convert.ToInt32(row["IdNguoiDung"]);
                nd.IdNhomNguoiDung = Convert.ToInt32(row["IdNhomNguoiDung"]);
                nd.Email = row["Email"].ToString().Trim();
                nd.TaiKhoan = row["TaiKhoan"].ToString().Trim();
                return nd;
            }
            return null;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

	public int Add(String taiKhoan, String matKhau, String hoTen, String chucVu, String diaChi, String email, String dienThoai, String diDong, int idBoPhan, int idNhomNguoiDung, int idCuaHang)
	{
		try
		{
			int result = SqlHelper.ExecuteNonQuery(Common.Conn,"tblNguoiDung_Add",taiKhoan,matKhau,hoTen,chucVu,diaChi,email,dienThoai,diDong,idBoPhan,idNhomNguoiDung,idCuaHang);
			return result;
		}
		catch(Exception e) {return -1; }
	}

	public int Update(int idNguoiDung, String taiKhoan, String matKhau, String hoTen, String chucVu, String diaChi, String email, String dienThoai, String diDong, int idBoPhan, int idNhomNguoiDung, int idCuaHang)
	{
		try
		{
			int result = SqlHelper.ExecuteNonQuery(Common.Conn,"tblNguoiDung_Update",idNguoiDung,taiKhoan,matKhau,hoTen,chucVu,diaChi,email,dienThoai,diDong,idBoPhan,idNhomNguoiDung,idCuaHang);
			return result;
		}
		catch(Exception e) {return -1; }
	}

	public int Delete(int idNguoiDung)
	{
		try
		{
			int result = SqlHelper.ExecuteNonQuery(Common.Conn,"tblNguoiDung_Delete",idNguoiDung);
			return result;
		}
		catch(Exception e) {return -1; }
	}

	public int DeleteAll()
	{
		try
		{
			int result = SqlHelper.ExecuteNonQuery(Common.Conn,"tblNguoiDung_DeleteAll");
			return result;
		}
		catch(Exception e) {return -1; }
	}

	public DataSet Get(int idNguoiDung)
	{
		try
		{
			DataSet result = SqlHelper.ExecuteDataset(Common.Conn,"tblNguoiDung_Get",idNguoiDung);
			return result;
		}
		catch(Exception e) {return null; }
	}

	public DataSet GetAll()
	{
		try
		{
			DataSet result = SqlHelper.ExecuteDataset(Common.Conn,"tblNguoiDung_GetAll");
			return result;
		}
		catch(Exception e) {return null; }
	}

	public DataSet GetByIdBoPhan(int idBoPhan)
	{
		try
		{
			DataSet result = SqlHelper.ExecuteDataset(Common.Conn,"tblNguoiDung_GetByIdBoPhan",idBoPhan);
			return result;
		}
		catch(Exception e) {return null; }
	}

	public DataSet GetByIdNhomNguoiDung(int idNhomNguoiDung)
	{
		try
		{
			DataSet result = SqlHelper.ExecuteDataset(Common.Conn,"tblNguoiDung_GetByIdNhomNguoiDung",idNhomNguoiDung);
			return result;
		}
		catch(Exception e) {return null; }
	}

	public DataSet GetByIdCuaHang(int idCuaHang)
	{
		try
		{
			DataSet result = SqlHelper.ExecuteDataset(Common.Conn,"tblNguoiDung_GetByIdCuaHang",idCuaHang);
			return result;
		}
		catch(Exception e) {return null; }
	}

}

public class NguoiDung
{
    private int idNguoiDung;
	public int IdNguoiDung
	{
		get { return idNguoiDung;}
		set { idNguoiDung = value;}
	}

	private String taiKhoan;
	public String TaiKhoan
	{
		get { return taiKhoan;}
		set { taiKhoan = value;}
	}

	private String matKhau;
	public String MatKhau
	{
		get { return matKhau;}
		set { matKhau = value;}
	}

	private String hoTen;
	public String HoTen
	{
		get { return hoTen;}
		set { hoTen = value;}
	}

	private String chucVu;
	public String ChucVu
	{
		get { return chucVu;}
		set { chucVu = value;}
	}

	private String diaChi;
	public String DiaChi
	{
		get { return diaChi;}
		set { diaChi = value;}
	}

	private String email;
	public String Email
	{
		get { return email;}
		set { email = value;}
	}

	private String dienThoai;
	public String DienThoai
	{
		get { return dienThoai;}
		set { dienThoai = value;}
	}

	private String diDong;
	public String DiDong
	{
		get { return diDong;}
		set { diDong = value;}
	}

	private int idBoPhan;
	public int IdBoPhan
	{
		get { return idBoPhan;}
		set { idBoPhan = value;}
	}

	private int idNhomNguoiDung;
	public int IdNhomNguoiDung
	{
		get { return idNhomNguoiDung;}
		set { idNhomNguoiDung = value;}
	}

	private int idCuaHang;
	public int IdCuaHang
	{
		get { return idCuaHang;}
		set { idCuaHang = value;}
	}

	public NguoiDung() {}

    public NguoiDung(int idNguoiDung, String taiKhoan, String matKhau, String hoTen, String chucVu, String diaChi, String email, String dienThoai, String diDong, int idBoPhan, int idNhomNguoiDung, int idCuaHang)
	 {
		this.idNguoiDung = idNguoiDung;
 		this.taiKhoan = taiKhoan;
 		this.matKhau = matKhau;
 		this.hoTen = hoTen;
 		this.chucVu = chucVu;
 		this.diaChi = diaChi;
 		this.email = email;
 		this.dienThoai = dienThoai;
 		this.diDong = diDong;
 		this.idBoPhan = idBoPhan;
 		this.idNhomNguoiDung = idNhomNguoiDung;
 		this.idCuaHang = idCuaHang;
 	}
}
}

